﻿using GalaSoft.MvvmLight;

namespace BankApplication.ViewModel
{
    using System.Security.Cryptography;
    using System.Windows.Controls;

    using BankApplication.BanksService;

    using GalaSoft.MvvmLight.Command;

    using HelpersAndContracts.Contracts;
    using HelpersAndContracts.Helpers;
    using System.Windows.Media;

    public class MainViewModel : ViewModelBase
    {
        private readonly IPaymentsService paymentsService;

        public RelayCommand SaveUserCommand { get; set; }
        public RelayCommand<Visual> PrintCommand { get; set; }

        private ClientModel clientToSave;

        public ClientModel ClientToSave
        {
            get
            {
                return clientToSave;
            }

            set
            {
                if (clientToSave == value)
                {
                    return;
                }

                clientToSave = value;
                RaisePropertyChanged("ClientToSave");
            }
        }

        private ClientModel clientResult;

        public ClientModel ClientResult
        {
            get
            {
                return clientResult;
            }

            set
            {
                if (clientResult == value)
                {
                    return;
                }

                clientResult = value;
                RaisePropertyChanged("ClientResult");
            }
        }

        private ECDiffieHellmanCng cryptographyAlgorithm = new ECDiffieHellmanCng();

        private Aes aes = new AesCryptoServiceProvider();

        private byte[] clientPublicKey;

        private byte[] clientPrivateKey;

        private static byte[] initializationVector;

        public MainViewModel()
        {
           paymentsService = new PaymentsServiceClient();
           SaveUserCommand = new RelayCommand(this.AddClient);
           PrintCommand = new RelayCommand<Visual>(this.Print);
           clientToSave = new ClientModel();
           ApplyProtocolAndExchangeKeys();
        }


        private void AddClient()
        {
            var res = ClientToSave.MapAndEncode<EncodedClientModel>(aes, clientPrivateKey);

            var client = paymentsService.AddUser(res);

            ClientResult = client.MapAndDecode<ClientModel>(clientPrivateKey, initializationVector);
        }

        private void Print(Visual clientInfo)
        {
            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() == true)
            {
                pd.PrintVisual(clientInfo, "Заявление");
            }
        }

        private void ApplyProtocolAndExchangeKeys()
        {
            cryptographyAlgorithm.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            cryptographyAlgorithm.HashAlgorithm = CngAlgorithm.Sha256;
            clientPublicKey = cryptographyAlgorithm.PublicKey.ToByteArray();
            var serverPublicKey = this.paymentsService.GetServerPublicKey("name");
            clientPrivateKey = cryptographyAlgorithm.DeriveKeyMaterial(CngKey.Import(serverPublicKey, CngKeyBlobFormat.EccPublicBlob));
            this.paymentsService.GenerateServerSecretKey(clientPublicKey, aes.IV);
            initializationVector = this.paymentsService.GetinitializationVector();
        }
        

    }
}